﻿using System;
using System.Data;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
using System.Collections.Generic;
using HRM.BusinessLogic;


namespace HRM
{
    public partial class frmRateList :FormBase
    {
        int pageSize = UIParamater.pageSize;
        int CurrPage = 0;
        int toltalPage = 0;
        int itemCount = 0;
        int _RateID = 0;
        RateClient _wcfRate;
        RoleRightInfo ListRole;


        public override void Show_Form()
        {
            this.Show();
        }

        protected override void Init_Control()
        {
            _wcfRate = Service.Rate();
            ListRole = UIParamater.Quyen_TyLeThuBaoHiem;
            if (!ListRole._create)
                btnNew.Enabled = false;
            if (!ListRole._delete)
                btnDel.Enabled = false;
            //
            Init_Grid();
            //
            if (!ListRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                btnEdit.Text = "xem";
            }
            //
            LoadData();
            this.KeyPreview = true;
        }

        public frmRateList()
        {
            InitializeComponent();       
        }

        //Khởi tạo danh sách cho grid
        DataGridHandle m_GridHandle;
        protected void Init_Grid()
        {
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();

            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "ROWNUMBER", 30, "", true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("XH", "Rate_Social", 50,Globals.CS_DISPLAY_RATE_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("YT", "Rate_Medical", 50, Globals.CS_DISPLAY_RATE_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("TN", "Rate_Unemployed", 50, Globals.CS_DISPLAY_RATE_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Còn lại", "Rate_Remain", 50, Globals.CS_DISPLAY_RATE_FORMAT));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mô tả", "Rate_Description", 200));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Mã chính", "Rate_ID", 0));
            //
            m_GridHandle = FormGlobals.Grid_Init(dgvList, arrColumn, "", 1);
            //m_GridHandle.Row_Validated += new DataGridHandle.Row_ValidatedEventHandler(m_GridHandle_Row_Validated);
        }

        //private void BuildVoucherGrid()
        //{
        //    UISetting.AddColumn(ref dgvList, "ROWNUMBER", "STT", "C", 30, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_ID", "Mã Chính", "C", 100, false, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Month", "Tháng/Năm", "L", 100, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Social", "XH", "L", 50, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Medical", "YT", "C", 50, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Unemployed", "TN", "C", 50, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Remain", "Còn lại", "C", 100, true, true);
        //    UISetting.AddColumn(ref dgvList, "Rate_Description", "Mô tả", "C", 200, true, true);
        //}

        void LoadData()
        {
            DataSet ds = Rate_Controller.Instance().Rate_GetList(out itemCount, CurrPage, pageSize);
            //PageIndex();
            dgvList.DataSource = ds.Tables[0];
            //dgvList.Rows.Clear();
            //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            //{
            //    dgvList.Rows.Insert(i, 1);

            //    dgvList.Rows[i].Cells["ROWNUMBER"].Value = ds.Tables[0].Rows[i]["ROWNUMBER"].ToString();
            //    dgvList.Rows[i].Cells["Rate_ID"].Value = ds.Tables[0].Rows[i]["Rate_ID"].ToString();
            //    dgvList.Rows[i].Cells["Rate_Month"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["Rate_Month"]).ToString("MM/yyyy");
            //    dgvList.Rows[i].Cells["Rate_Social"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["Rate_Social"].ToString()).ToString(UIParamater.Cham2So);
            //    dgvList.Rows[i].Cells["Rate_Medical"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["Rate_Medical"].ToString()).ToString(UIParamater.Cham2So);
            //    dgvList.Rows[i].Cells["Rate_Unemployed"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["Rate_Unemployed"].ToString()).ToString(UIParamater.Cham2So);
            //    dgvList.Rows[i].Cells["Rate_Remain"].Value = Convert.ToDecimal(ds.Tables[0].Rows[i]["Rate_Remain"].ToString()).ToString(UIParamater.Cham2So);
            //    dgvList.Rows[i].Cells["Rate_Description"].Value = ds.Tables[0].Rows[i]["Rate_Description"].ToString();
            //}
        }
        //private void PageIndex()
        //{
        //    toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
        //    if (toltalPage == 0)
        //        lblPageIndex.Text = "0" + "/" + toltalPage.ToString();
        //    else
        //        lblPageIndex.Text = (CurrPage + 1).ToString() + "/" + toltalPage.ToString();
        //}
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!ListRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmRateInfo(0, ListRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                CurrPage = 0;
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {

            if (dgvList.SelectedRows.Count > 0)
            {
                if (ListRole.Update || ListRole.View)
                {
                    _RateID = Convert.ToInt32(dgvList.CurrentRow.Cells["Rate_ID"].Value);
                    Form f = new frmRateInfo(_RateID, ListRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UpdateUI.GetString("canhbao"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!ListRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _RateID = Convert.ToInt32(dgvList.CurrentRow.Cells["Rate_ID"].Value);

                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfRate.Rate_Delete(_RateID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }

        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            CurrPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (CurrPage != 0)
                CurrPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (toltalPage > 0 && CurrPage != toltalPage - 1)
                CurrPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            toltalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (toltalPage != 0)
                CurrPage = toltalPage - 1;
            LoadData();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnEdit_Click(null, null);
            }
        }

        private void frmRateList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopUp.Enabled = true;
                    btnNewBranch.Enabled = true;
                    btnEditBranch.Enabled = true;
                }
            }
        }

        private void btnNewBranch_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditBranch_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void frmRateList_Activated(object sender, EventArgs e)
        {
          
            //if (dgvList.Columns.Count > 0)
            //{
            //    dgvList.Columns["Rate_Name"].HeaderText = UpdateUI.GetString("phucapluong");
            //}
        }

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}